package cj.library.utils;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 关于文本字符串的处理类
 * 输入流转字符串，字符串转输入流InputStream
 * 电话号码和邮箱验证，字符串加密解密
 * 字节数组转换成16进制字符串
 * 设置TextView文本高亮
 * 用html标签改变字体颜色
 * 字符串转成布尔型
 * 格式化时间 例如 6.5 变为 6:30
 * Created by cj on 2015/11/22.
 */
public class StringUtil {

	private static String key2 = "stringUtil";
	private static String key1 = "123456";
	/** 年月日时分秒 */
	public final static String FORMAT_YMDHMS = "yyyy-MM-dd kk:mm:ss";

	/** 获得当前时间 */
	public static CharSequence currentTime(CharSequence inFormat) {
		return DateFormat.format(inFormat, System.currentTimeMillis());
	}

	public static String getWebCon(String domain) {
		// System.out.println("开始读取内容...("+domain+")");
		StringBuffer sb = new StringBuffer();
		try {
			java.net.URL url = new java.net.URL(domain);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream()));
			String line;
			while ((line = in.readLine()) != null) {
				sb.append(line);
			}
			in.close();
		} catch (Exception e) { // Report any errors that arise
			sb.append(e.toString());
			System.err.println(e);
			System.err
					.println("Usage:   java   HttpClient   <URL>   [<filename>]");
		}
		return sb.toString();
	}

	public  static String getFromAssets(Context context, String fileName) {
		try {
			InputStreamReader inputReader = new InputStreamReader(context
					.getResources().getAssets().open(fileName));
			BufferedReader bufReader = new BufferedReader(inputReader);
			String line = "";
			String Result = "";
			while ((line = bufReader.readLine()) != null)
				Result += line;
			return Result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 字符串转stream
	 * 
	 * @param str
	 * @return
	 */
	public static InputStream StringToInputStream(String str) {
		if (TextUtils.isEmpty(str))
			return null;
		ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
		return stream;
	}

	/**
	 * 输入流转字符串
	 * @param is  输入流
	 * @param charset  编码类型(一般都是UTF-8)
	 * @return
	 */
	public static String inputStreamToString(InputStream is,String charset) {
		StringBuilder sb = new StringBuilder();
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(is,charset));

			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return sb.toString();
	}

	/**
	 * 字符串替换
	 * 
	 * @param line
	 * @param oldString
	 * @param newString
	 * @return
	 */
	public static final String replace(String line, String oldString,
			String newString) {
		if (line == null) {
			return null;
		}
		int i = 0;
		if ((i = line.indexOf(oldString, i)) >= 0) {
			char[] line2 = line.toCharArray();
			char[] newString2 = newString.toCharArray();
			int oLength = oldString.length();
			StringBuffer buf = new StringBuffer(line2.length);
			buf.append(line2, 0, i).append(newString2);
			i += oLength;
			int j = i;
			while ((i = line.indexOf(oldString, i)) > 0) {
				buf.append(line2, j, i - j).append(newString2);
				i += oLength;
				j = i;
			}
			buf.append(line2, j, line2.length - j);
			return buf.toString();
		}
		return line;
	}

	/**
	 * 检查是否符合手机号码格式 用正则表达式匹配输入的手机号码是否正确
	 * 
	 * @param ph1Num
	 * @return
	 */
	public static boolean isMobileNO(String ph1Num) {
		// Pattern p =
		// Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
	//	final String regx = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
		String telRegex = "[1][3458]\\d{9}";//
		Pattern p = Pattern.compile(telRegex);
		Matcher m = p.matcher(ph1Num);

		return m.matches();
	}

	/**
	 * 固定电话号码验证
	 * 
	 * @param str
	 * @return 验证通过返回true
	 */
	public static boolean isPh1No(String str) {
		Pattern p1 = null, p2 = null, p3 = null;
		Matcher m = null;
		boolean b = false;
		p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{6,10}$"); // 验证带区号的
		p2 = Pattern.compile("^[1-9]{1}[0-9]{6,8}$"); // 验证没有区号的
		p3 = Pattern.compile("^[0][1-9]{2,3}[0-9]{6,10}$"); // 验证带区号的
		if (str.length() > 9) {
			if (str.contains("-")) {
				m = p1.matcher(str);
				b = m.matches();
			} else {
				m = p3.matcher(str);
				b = m.matches();
			}
		} else {

			m = p2.matcher(str);
			b = m.matches();

		}
		return b;
	}
	
	/**
	 * 字符串加密
	 * 
	 * @param password
	 * @return
	 */
	public static String encryptionKey(String password) {
		byte[] keyByte1 = key1.getBytes();
		byte[] keyByte2 = key2.getBytes();
		byte[] pwdByte = password.getBytes();
		for (int i = 0; i < pwdByte.length; i++) {
			pwdByte[i] = (byte) (pwdByte[i] ^ keyByte1[i % keyByte1.length]);
		}
		byte[] countByte = new byte[pwdByte.length + keyByte1.length];
		for (int i = 0; i < countByte.length; i++) {
			if (i < pwdByte.length)
				countByte[i] = pwdByte[i];
			else
				countByte[i] = keyByte1[i - pwdByte.length];
		}
		for (int i = 0; i < countByte.length; i++) {
			countByte[i] = (byte) (countByte[i] ^ keyByte2[i % keyByte2.length]);
		}
		return bytesToHexString(countByte);
	}

	/**
	 * 字符串解密
	 * 
	 * @param password
	 * @return
	 */
	public static String decryptionKey(String password) {
		byte[] keyByte1 = key1.getBytes();
		byte[] keyByte2 = key2.getBytes();
		// password = hexStr2Str(password);
		byte[] pwdByte = hexStr2Bytes(password);

		for (int i = 0; i < pwdByte.length; i++) {
			pwdByte[i] = (byte) (pwdByte[i] ^ keyByte2[i % keyByte2.length]);
		}

		byte[] lastByte = new byte[pwdByte.length - keyByte1.length];
		for (int i = 0; i < lastByte.length; i++) {
			lastByte[i] = pwdByte[i];
		}
		for (int i = 0; i < lastByte.length; i++) {
			lastByte[i] = (byte) (lastByte[i] ^ keyByte1[i % keyByte1.length]);
		}

		return new String(lastByte);
	}

	/**
	 * 把字节数组转换成16进制字符串
	 * 
	 * @param bArray
	 * @return
	 */
	public static final String bytesToHexString(byte[] bArray) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		for (int i = 0; i < bArray.length; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}

	/**
	 * 判断电话号码格式是否正确(包括手机和座机)
	 * @param ph1num
	 * @return
	 */
	public static boolean isPh1Num(String ph1num) {
		boolean flag = false;
		if (StringUtil.isMobileNO(ph1num) || StringUtil.isPh1No(ph1num)) {
			flag = true;
		}
		return flag;
	}

	/**
	 * 十六进制转换字符串
	 * 
	 * @param
	 *
	 * @return String 对应的字符串
	 */
	public static String hexStr2Str(String hexStr) {
		String str = "0123456789ABCDEF";
		char[] hexs = hexStr.toCharArray();
		byte[] bytes = new byte[hexStr.length() / 2];
		int n;

		for (int i = 0; i < bytes.length; i++) {
			n = str.indexOf(hexs[2 * i]) * 16;
			n += str.indexOf(hexs[2 * i + 1]);
			bytes[i] = (byte) (n & 0xff);
		}
		return new String(bytes);
	}

	/**
	 * bytes字符串转换为Byte值
	 * 
	 * @param
	 *
	 * @return byte[]
	 */
	public static byte[] hexStr2Bytes(String src) {
		int m = 0, n = 0;
		int l = src.length() / 2;
		byte[] ret = new byte[l];
		for (int i = 0; i < l; i++) {
			m = i * 2 + 1;
			n = m + 1;
			ret[i] = Byte.decode("0x" + src.substring(i * 2, m)
					+ src.substring(m, n));
		}
		return ret;
	}

	/**
	 * 设置TextView字体高亮，需先调用setTextColor(int color);
	 * @param start 开始位置
	 * @param end   结束位置
	 * @param text  textview
	 * @param color 颜色
	 */
	public static void setTextHighLight(int start, int end, TextView text,int color) {
		SpannableStringBuilder spannable = new SpannableStringBuilder(text
				.getText().toString());
		ForegroundColorSpan span = new ForegroundColorSpan(color);
		spannable.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		text.setText(spannable);
	}

	/**
	 * 设置字符串颜色
	 * 如：<font color='#2B86E3'>" + 你好 + "</font> + "string"
	 * @param text   字符串
	 * @param color  颜色
	 * @return
	 */
	public static String setStrColor(String text,int color) {
		if (text != null) {
			StringBuilder sb = new StringBuilder();
			sb.append("<font color='" + color + "'>" + text + "</font>");
			return sb.toString();
		} else {
			return "";
		}
	}

	public static String getHightLightText(String text) {
		Pattern p = Pattern.compile("<em>([^</em>]*)");// 匹配<title>开头，</title>结尾的文档
		Matcher m = p.matcher(text);// 开始编译
		String keyword = null;
		while (m.find()) {
		//	LogUtils.d("hight light:", m.group(1));
			keyword = m.group(1);
		}
		return keyword;
	}

	/**
	 * 判断email格式是否正确
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(email);

		return m.matches();
	}

	/**
	 * 格式化时间 例如 6.5 变为 6:30
	 * @param dateString
	 *            字符串格式的时间
	 * @return
	 */
	public static String toTimeString(String dateString) {
		String result = "";
		int index = dateString.indexOf(".");
		if (index == -1) {
			result = dateString + ":" + "00";
		} else {
			String min = dateString.substring(index + 1);
			int intMin = Integer.parseInt(min) * 6;
			String formatMin = intMin < 10 ? "0" + intMin : "" + intMin;
			result = dateString.substring(0, index) + ":" + formatMin;
		}
		return result;
	}

	/**
	 * 字符串转成布尔型
	 *
	 * @param value
	 *            ：True或1为true
	 * @return
	 */
	public static Boolean parseBoolean(String value) {
		Boolean result = false;
		if (value.toLowerCase().equals("true") || value.equals("1")) {
			result = true;
		}
		return result;
	}
}
